<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/ordermaker/pages/layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    #include("common.html")

    <div class="layui-body">
        <div style="margin-left: 10px;margin-right: 10px;margin-top: 15px;">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addone">添加数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="import" id="uploadFile">批量导入</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="repass">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/ordermaker/pages/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use(['element','table','upload'], function(){
        var element = layui.element;
        var table = layui.table;

        var tableIns=table.render({
            elem: '#test'
            ,url:'/ordermaker/admin/user/list'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {field:'id', title:'ID', fixed: 'left', unresize: true}
                ,{field:'name', title:'用户名', width:120}
                ,{field:'email', title:'邮箱', edit: 'email', templet: function(res){
                        return '<em>'+ res.email +'</em>'
                    }}
                ,{field:'when', title:'加入时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,parseData: function(res){ //res 即为原始返回的数据
                console.log(res)
                return {
                    "code": res.erro, //解析接口状态
                    "count": res.datas.totalRow, //解析数据长度
                    "data": res.datas.list //解析数据列表
                };
            }
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'addone':
                    layer.open({
                        type: 2,
                        title: 'layer mobile页',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['380px', '300px'],
                        content: '/ordermaker/pages/user-add.html',
                        end:function(index, layero){
                            location.reload();
                        }
                    });
                    break;
            };
        });

        //监听单元格编辑
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var updateData={'user.id':data.id,'user.email':data.email};
            layui.jquery.ajax({
                url: "/ordermaker/admin/user/edt",
                type: 'post',//method请求方式，get或者post
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                xhrFields:{
                    withCredentials:true
                },
                dataType: 'json',//预期服务器返回的数据类型
                data: updateData,//表格数据序列化
                success: function (res) {//res为相应体,function为回调函数
                    console.log(res);
                    layer.msg(data.name+"的"+field+"已修改为"+value);
                },
                error: function (res) {
                    console.log(res);
                    layer.alert('操作失败！！！', {icon: 5});
                }
            });
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    layui.jquery.ajax({
                        url: "/ordermaker/admin/user/del",
                        type: 'post',//method请求方式，get或者post
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        xhrFields:{
                            withCredentials:true
                        },
                        dataType: 'json',//预期服务器返回的数据类型
                        data: {id:data.id},//表格数据序列化
                        success: function (res) {//res为相应体,function为回调函数
                            console.log(res);
                            layer.msg(data.name+"已删除");
                            obj.del();
                            layer.close(index);
                        },
                        error: function (res) {
                            console.log(res);
                            layer.alert('操作失败！！！', {icon: 5});
                        }
                    });
                });
            } else if(obj.event === 'repass'){
                layui.jquery.ajax({
                    url: "/ordermaker/admin/user/repass",
                    type: 'post',//method请求方式，get或者post
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    xhrFields:{
                        withCredentials:true
                    },
                    dataType: 'json',//预期服务器返回的数据类型
                    data: {id:data.id},//表格数据序列化
                    success: function (res) {//res为相应体,function为回调函数
                        console.log(res);
                        layer.msg(data.name+"的秘密重置成功")
                    },
                    error: function (res) {
                        console.log(res);
                        layer.alert('操作失败！！！', {icon: 5});
                    }
                });
            }
        });

        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#uploadFile' //绑定元素
            ,url: '/ordermaker/admin/user/upload' //上传接口
            ,accept: 'file' //普通文件
            ,exts: 'xls|xlsx' //只允许上传压缩文件
            ,done: function(res){
                //上传完毕回调
                if(res.erro===0){
                    layer.alert("成功导入"+res.msg+"条数据");
                    tableIns.reload();
                }else{
                    layer.alert("数据解析失败");
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>
</body>
</html>